﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.Collections.Generic;
using AccessControl.Contracts.Models;
using System.Windows.Media;
using System.Globalization;
using AccessControl.Wpf.Converters;

namespace AccessControl.Wpf.Printing
{
	class EventPageElement : UserControl
	{
		#region Fields

		private const int s_PageMargin = 75;
		private const int s_HeaderHeight = 25;
		private const int s_LineHeight = 20;
		private const int s_ColumnWidth = 100;
		
		private static SensorToRelatedEntryConverter _converter = new SensorToRelatedEntryConverter();

		#endregion

		#region Public Constructor

		public EventPageElement()
		{
			Margin = new Thickness(s_PageMargin);
		}

		#endregion

		#region Public Properties

		public int CurrentRow { get; set; }
		public int Rows { get; set; }
		public IList<IEvent> Events { get; set; }

		#endregion

		#region Public Members

		public static int RowsPerPage(double height)
		{
			return (int)Math.Floor((height - (2 * s_PageMargin) - s_HeaderHeight) / s_LineHeight);
		}

		#endregion

		#region Overrides

		protected override void OnRender(DrawingContext dc)
		{
			IEvent myEvent;
			Point curPoint = new Point(0, 0);

			/* Entry */
			dc.DrawText(MakeText("Entry"), curPoint);
			curPoint.X += s_ColumnWidth;
			/* Time */
			dc.DrawText(MakeText("Time"), curPoint);
			curPoint.X += (s_ColumnWidth * 2);
			/* Raised by sensor */
			dc.DrawText(MakeText("Raised by"), curPoint);
			curPoint.X += s_ColumnWidth;
			/* Handled */
			dc.DrawText(MakeText("Handled"), curPoint);
			curPoint.X += s_ColumnWidth;
			/* Handling operator */
			dc.DrawText(MakeText("Handling operator"), curPoint);
			curPoint.X += s_ColumnWidth;

			/* Reset */
			curPoint.X = 0;
			curPoint.Y += s_LineHeight;
			
			/* Seperator */
			dc.DrawRectangle(Brushes.Black, null, new Rect(curPoint, new Size(Width, 2)));
			curPoint.Y += s_HeaderHeight - s_LineHeight;

			for (int i = CurrentRow; i < CurrentRow + Rows; i++)
			{
				/* Get the event */
				myEvent = Events[i];
				
				/* Print the entry name */
				dc.DrawText(MakeText(_converter.Convert(myEvent.AssociatedSensor, null, "Name", null) as string), curPoint);
				curPoint.X += s_ColumnWidth;

				/* Print the time */
				dc.DrawText(MakeText(myEvent.EventTime.ToString()), curPoint);
				curPoint.X += (s_ColumnWidth * 2);

				/* Print sensor type */
				dc.DrawText(MakeText(myEvent.AssociatedSensor.Type.ToString()), curPoint);
				curPoint.X += s_ColumnWidth;

				/* Print whether handled */
				dc.DrawText(MakeText(myEvent.Handled.ToString()), curPoint);
				curPoint.X += s_ColumnWidth;

				/* And the handling operator */
				dc.DrawText(MakeText(myEvent.HandlingOperator), curPoint);
				curPoint.X += s_ColumnWidth;

				/* And finally go down one line */
				curPoint.X = 0;
				curPoint.Y += s_LineHeight;
			}
		}

		#endregion

		#region Private Members

		private static FormattedText MakeText(string text)
		{
			return new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Tahoma"), 14, Brushes.Black);
		}

		#endregion
	}
}
